package com.cndw.kungfu.model.pvp.campcross;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.StringProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.battle.Fighter;
import com.cndw.kungfu.model.battle.FighterBaseCross;
import com.cndw.kungfu.model.battle.FighterCrossPlayer;
import com.cndw.kungfu.model.battle.FighterTeam;
import com.cndw.kungfu.model.pvp.war.WarFighter;
import com.cndw.kungfu.vo.battlevo.TeamCampVO;

public class CampTeam implements FighterTeam, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected String[] names;

	protected int[] levels;

	protected long[] uids;

	private int[] powers;

	protected int[] vids;

	protected int sid;

	private int win;

	private int all;

	private int streak;
	/**
	 * 这里是战力
	 */
	private int power;

	protected boolean source;

	private int level;

	protected transient Collection<Fighter> fighters;
	protected transient Fighter master;

	public CampTeam() {

	}

	public long getMasterId() {
		return uids[0];
	}

	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	public int getWin() {
		return win;
	}

	public void setWin(int win) {
		this.win = win;
	}

	public int getAll() {
		return all;
	}

	public void setAll(int all) {
		this.all = all;
	}

	public int getStreak() {
		return streak;
	}

	public void setStreak(int streak) {
		this.streak = streak;
	}

	public int getPower() {
		return power;
	}

	public boolean isSource() {
		return source;
	}

	public void setSource(boolean source) {
		this.source = source;
	}

	public String getIds() {
		return StringProcesser.toString(uids);
	}

	@Override
	public Collection<Fighter> getBattleMembers() {
		return fighters;
	}

	@Override
	public Fighter getMaster() {
		return master;
	}

	public void notifyWin(boolean isWin) {
		all++;
		if (isWin) {
			win++;
			streak++;
		} else {
			streak = 0;
		}

	}

	public String getMasterName() {
		return names[0];
	}

	public int getLevel() {
		return level;
	}

	public Object[] getObj() {
		return new Object[] { source, sid, uids[0], names[0] };
	}

	public void updateFighter(Collection<WarFighter> warFighters) {
		if (warFighters != null) {
			List<Fighter> currentFighters = new ArrayList<Fighter>();
			power = 0;
			level = 0;
			List<Fighter> currentPet = new ArrayList<Fighter>();
			for (WarFighter warFighter : warFighters) {
				if (warFighter.getCrossPlayer() == null) {
					LogUtil.getMain().error("err "+getIds());
					continue;
				}
				currentFighters.add(warFighter.getCrossPlayer());
				if (warFighter.getCrossPet() != null) {
					currentPet.add(warFighter.getCrossPet());
				}
				if (warFighter.getPid() == getMasterId()) {
					master = warFighter.getCrossPlayer();
				}
				power += warFighter.getScore();
				level += warFighter.getLevel();
			}
			currentFighters.addAll(currentPet);
			fighters = currentFighters;
			level = level / 3;
		}
	}

	public String[] getNames() {
		return names;
	}

	public void setNames(String[] names) {
		this.names = names;
	}

	public int[] getLevels() {
		return levels;
	}

	public void setLevels(int[] levels) {
		this.levels = levels;
	}

	public long[] getUids() {
		return uids;
	}

	public void setUids(long[] uids) {
		this.uids = uids;
	}

	public int[] getPowers() {
		return powers;
	}

	public void setPowers(int[] powers) {
		this.powers = powers;
	}

	public int[] getVids() {
		return vids;
	}

	public void setVids(int[] vids) {
		this.vids = vids;
	}

	public CampTeam(LinkedList<Long> ids) {
		Player member0 = Context.getPlayerServer().getByPlayerId(ids.pollFirst());
		Player member1 = Context.getPlayerServer().getByPlayerId(ids.pollFirst());
		Player member2 = Context.getPlayerServer().getByPlayerId(ids.pollFirst());
		sid = ConstantsConfig.getSysCgServiceId();
		uids = new long[] { member0.getPlayerId(), member1.getPlayerId(), member2.getPlayerId() };
		levels = new int[] { member0.getLevel(), member1.getLevel(), member2.getLevel() };
		names = new String[] { member0.getPlayerName(), member1.getPlayerName(), member2.getPlayerName() };
		vids = new int[] { member0.getVocationId(), member1.getVocationId(), member2.getVocationId() };
	}

	public TeamCampVO toCampVO() {
		TeamCampVO e = new TeamCampVO();
		e.setPlayerId(getMasterId());
		e.setPlayerName(getMasterName());
		List<Object> members = new ArrayList<Object>();
		for (int i = 0; i < uids.length; i++) {
			members.add(new Object[] { uids[i], names[i], levels[i], vids[i] });
		}
		e.setMembers(members);
		e.setSid(sid);
		e.setScore(win);
		return e;
	}
	
	public void reset(){
		for (Fighter e : fighters) {
			try {
				if ( e instanceof FighterBaseCross) {
					((FighterBaseCross) e).fullHpMp();
				}
				if (e instanceof FighterCrossPlayer) {
					((FighterCrossPlayer) e).resetSkillId();
				}	
			} catch (Exception e2) {
				e2.printStackTrace();
			}

		}
	}

}
